package com.wgchao.mdmp.common.http;

import com.alibaba.fastjson.JSON;
import com.arronlong.httpclientutil.builder.HCB;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.common.HttpMethods;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @作者：wgchao
 * @时间：16:16 2019/8/18 0018
 * @用途：http参数
 **/
@Data
public class HttpConfigParam implements Serializable {
    private String url;
    private String method;//发送方式get/post/put/delete
    private Map<String, Object> gparams = new HashMap<>();//get方式参数
    private Object pparam;//json参数
    private Map<String, String> headers = new HashMap<>();;//header头部信息设置
    private int outtime = 1000;//超时时间
    private int retry = 3;// 重试次数
    private String encode = "utf-8";//编码
    private Header[] headerarr;

    public static HttpConfigParam instanceOfGet(String url){
        HttpConfigParam httpConfig = new HttpConfigParam();
        httpConfig.url = url;
        httpConfig.method = "get";
        return httpConfig;
    }

    public static HttpConfigParam instanceOfPost(String url){
        HttpConfigParam httpConfig = new HttpConfigParam();
        httpConfig.url = url;
        httpConfig.method = "post";
        return httpConfig;
    }

    public static HttpConfigParam instanceOfCustom(String url, String method){
        HttpConfigParam httpConfig = new HttpConfigParam();
        httpConfig.url = url;
        httpConfig.method = method;
        return httpConfig;
    }

    public HttpConfigParam putDataToMap(String key, Object value){
        this.gparams.put(key, value);
        return this;
    }

    public HttpConfigParam putDataToJson(Object obj){
        this.pparam = obj;
        return this;
    }

    public HttpConfigParam setOutTime(int outtime){
        this.outtime = outtime;
        return this;
    }

    public HttpConfigParam setretry(int retry){
        this.retry = retry;
        return this;
    }

    public HttpConfigParam setencode(String encode){
        this.encode = encode;
        return this;
    }

    public HttpConfigParam putHeader(String key, String value){
        this.headers.put(key, value);
        return this;
    }

    /**
     * 插件式配置Header（各种header信息、自定义header）
     * @return
     */
    public void getHeadersInfo(FunctionInterface functionInterface){
        if(!this.headers.isEmpty()){
            HttpHeader httpHeader = HttpHeader.custom();
            for(Map.Entry<String, String> entry:this.headers.entrySet()){
                httpHeader = functionInterface.setHeaderInfo(httpHeader, entry.getKey(), entry.getValue());
            }
            this.headerarr = httpHeader.build();
        }else{
            this.headerarr = null;
        }
    }

    /**
     * //插件式配置生成HttpClient时所需参数（超时、连接池、ssl、重试）
     * @return
     */
    public HCB getHCB(){
        return  HCB.custom().retry(this.retry);
    }

    /**
     * //插件式配置请求参数（网址、请求参数、编码、client）
     * @return
     * @throws Exception
     */
    public HttpConfig getConfig() throws Exception{
        HttpConfig config = HttpConfig.custom();
        //设置headers
        if(this.headerarr!=null && this.headerarr.length>0){
            config = config.headers(this.headerarr);
        }
        //超时
        if(this.outtime > 0){
            config = config.timeout(this.outtime);
        }
        //设置请求的url
        if(!StringUtils.isEmpty(this.url)){
            config = config.url(this.url);
        }else{
            throw new Exception("URL缺失");
        }
        //设置method 默认get方式
        if(StringUtils.isNotBlank(this.method)){
            if("get".equals(this.method.toLowerCase())){
                config = config.method(HttpMethods.GET);
            }else if("post".equals(this.method.toLowerCase())){
                config = config.method(HttpMethods.POST);
            }else if("put".equals(this.method.toLowerCase())){
                config = config.method(HttpMethods.PUT);
            }else if("delete".equals(this.method.toLowerCase())){
                config = config.method(HttpMethods.DELETE);
            }else{
                config = config.method(HttpMethods.GET);
            }
        }else{
            config = config.method(HttpMethods.GET);
        }
        //设置请求参数
        if(!this.gparams.isEmpty()){
            config = config.map(this.gparams);
        }
        //设置请求和返回编码
        config = config.encoding(this.encode);
        if(this.pparam != null){
            config = config.json(JSON.toJSONString(this.pparam));
        }
        config = config.client(getHCB().build());
        return config;

    }

}
